package com.lfg.leetcode.leetcode0;

public class No61rotateRight {

    public static void main(String[] args) {
        ListNode ret = rotateRight(ListNode.deserialize("[1,2]"), 2);
        System.out.println(ret);
    }

    public static ListNode rotateRight(ListNode head, int k) {
        if (head == null || head.next == null || k == 0) {
            return head;
        }

        ListNode ret = null;
        int size = 1;
        ListNode node = head;
        while (node.next != null) {
            size++;
            node = node.next;
        }
        ListNode tail = node;
        k = k % size;
        if (k == 0) {
            return head;
        }
        k = size - k;
        node = head;
        while (k-- > 1) {
            node = node.next;
        }
        ret = node.next;
        node.next = null;
        tail.next = head;
        return ret;
    }
}

